<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="en,default">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="google-site-verification" content="neREdgy0YHiEBAV4iCGPb8V3dj0F8dDMQ2OYRpRQKyo" />














  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Swift,OAuth,微博," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.2" />






<meta name="description" content="网络请求Alamofire + SwiftyJSON">
<meta property="og:type" content="article">
<meta property="og:title" content="Swift 的 新浪微博 OAuth 认证实现">
<meta property="og:url" content="https://didee.cn/2015/11/29/Swift-Weibo-OAuth/index.html">
<meta property="og:site_name" content="Dee's Blog">
<meta property="og:description" content="网络请求Alamofire + SwiftyJSON">
<meta property="og:image" content="https://didee.cn/images/14804085876901.jpg">
<meta property="og:image" content="https://didee.cn/images/14804086149366.jpg">
<meta property="og:image" content="https://didee.cn/images/14804086315231.jpg">
<meta property="og:image" content="https://didee.cn/images/14804086422144.jpg">
<meta property="og:updated_time" content="2017-02-10T03:24:48.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Swift 的 新浪微博 OAuth 认证实现">
<meta name="twitter:description" content="网络请求Alamofire + SwiftyJSON">
<meta name="twitter:image" content="https://didee.cn/images/14804085876901.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.0.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://didee.cn/2015/11/29/Swift-Weibo-OAuth/"/>





  <title>Swift 的 新浪微博 OAuth 认证实现 | Dee's Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="en">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Dee's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            About
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://didee.cn/2015/11/29/Swift-Weibo-OAuth/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Dee">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Dee's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Swift 的 新浪微博 OAuth 认证实现</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2015-11-29T16:31:00+08:00">
                2015-11-29
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Swift/" itemprop="url" rel="index">
                    <span itemprop="name">Swift</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="网络请求"><a href="#网络请求" class="headerlink" title="网络请求"></a>网络请求</h2><h3 id="Alamofire-SwiftyJSON"><a href="#Alamofire-SwiftyJSON" class="headerlink" title="Alamofire + SwiftyJSON"></a>Alamofire + SwiftyJSON</h3><a id="more"></a>
<p><a href="https://github.com/Alamofire/Alamofire" target="_blank" rel="external">Alamofire</a><br><a href="https://github.com/SwiftyJSON/SwiftyJSON" target="_blank" rel="external">SwiftyJSON</a></p>
<p><strong>封装网络请求</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> UIKit</div><div class="line"><span class="keyword">import</span> Alamofire</div><div class="line"><span class="keyword">import</span> SwiftyJSON</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">HTTPTool</span></span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">static</span> <span class="keyword">let</span> shared = <span class="type">HTTPTool</span>()</div><div class="line">    </div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">extension</span> <span class="title">HTTPTool</span> </span>&#123;</div><div class="line">    </div><div class="line">    <span class="comment">/// 网络请求方法</span></div><div class="line">    <span class="comment">///</span></div><div class="line">    <span class="comment">/// - Parameters:</span></div><div class="line">    <span class="comment">///   - url: url</span></div><div class="line">    <span class="comment">///   - method: method</span></div><div class="line">    <span class="comment">///   - params: params</span></div><div class="line">    <span class="comment">///   - success: 请求成功: 返回SwiftyJSON对象(可直接取子对象)</span></div><div class="line">    <span class="comment">///   - errors: errors</span></div><div class="line">    <span class="function"><span class="keyword">func</span> <span class="title">requestWithParameters</span><span class="params">(url: String, method: HTTPMethod, parameters params: Parameters, success: @escaping <span class="params">(<span class="number">_</span> json: JSON)</span></span></span>-&gt;<span class="type">Void</span>, errors: @escaping ()-&gt;<span class="type">Void</span>) &#123;</div><div class="line">        </div><div class="line">        <span class="type">Alamofire</span>.request(url, method: method, parameters: params).responseJSON &#123; (response) <span class="keyword">in</span></div><div class="line">            <span class="keyword">switch</span> response.result &#123;</div><div class="line">                </div><div class="line">            <span class="keyword">case</span> .success:</div><div class="line">                <span class="keyword">if</span> <span class="keyword">let</span> value = response.result.value&#123;</div><div class="line">                    <span class="keyword">let</span> json = <span class="type">JSON</span>(value)</div><div class="line">                    </div><div class="line">                    <span class="keyword">guard</span> json[<span class="string">"_error"</span>].string == <span class="literal">nil</span> <span class="keyword">else</span>&#123;</div><div class="line">                        errors()</div><div class="line">                        <span class="keyword">return</span></div><div class="line">                    &#125;</div><div class="line">                    success(json)</div><div class="line">                &#125;</div><div class="line">                <span class="keyword">break</span></div><div class="line">                </div><div class="line">            <span class="keyword">case</span> .failure(<span class="keyword">let</span> error):</div><div class="line">                <span class="type">NSLog</span>(<span class="string">"error,failure: <span class="subst">\(error)</span>"</span>)</div><div class="line">                errors()</div><div class="line">                <span class="keyword">break</span></div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="OAuth认证"><a href="#OAuth认证" class="headerlink" title="OAuth认证"></a>OAuth认证</h2><h3 id="WebView加载授权网页"><a href="#WebView加载授权网页" class="headerlink" title="WebView加载授权网页"></a>WebView加载授权网页</h3><h4 id="OAuth2的authorize接口"><a href="#OAuth2的authorize接口" class="headerlink" title="OAuth2的authorize接口"></a>OAuth2的authorize接口</h4><p><img src="/images/14804085876901.jpg" width="728"></p>
<h4 id="获取URL"><a href="#获取URL" class="headerlink" title="获取URL"></a>获取URL</h4><p>定义 client_id 和 redirect_uri 为常量</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">fileprivate <span class="keyword">let</span> client_id = <span class="string">""</span></div><div class="line">fileprivate <span class="keyword">let</span> redirect_uri = <span class="string">""</span></div></pre></td></tr></table></figure>
<p>拼接URL</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> oAuthURL:<span class="type">String</span> &#123;</div><div class="line">    <span class="keyword">return</span> <span class="string">"https://api.weibo.com/oauth2/authorize?client_id=<span class="subst">\(client_id)</span>&amp;redirect_uri=<span class="subst">\(redirect_uri)</span>&amp;display=mobile&amp;forcelogin=true"</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="加载授权网页"><a href="#加载授权网页" class="headerlink" title="加载授权网页"></a>加载授权网页</h4><figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">webView.loadRequest(request)</div></pre></td></tr></table></figure>
<h3 id="截取code"><a href="#截取code" class="headerlink" title="截取code"></a>截取code</h3><h4 id="WebView代理"><a href="#WebView代理" class="headerlink" title="WebView代理"></a>WebView代理</h4><p>遵守协议并实现 shouldStartLoad 方法</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">extension</span> <span class="title">WBOAuthViewController</span>: <span class="title">UIWebViewDelegate</span></span>&#123;</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">func</span> <span class="title">webView</span><span class="params">(<span class="number">_</span> webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType)</span></span> -&gt; <span class="type">Bool</span> &#123;</div><div class="line">    </div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="截取code字段"><a href="#截取code字段" class="headerlink" title="截取code字段"></a>截取code字段</h4><p>code是跟随返回地址的参数</p>
<p><img src="/images/14804086149366.jpg" width="713"></p>
<p>实现 shouldStartLoad 方法后可以得到所有网络请求 , 若请求包含字段 error_uri , 则授权失败 , 若请求字段包含 code , 则授权成功 , 得到正确的 code</p>
<p>是否包含字段</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> urlString = request.url?.query, urlString.hasPrefix(<span class="string">"error_uri or code"</span>)</div></pre></td></tr></table></figure>
<p>截取code字段</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> code = urlString.substring(from: <span class="string">"code="</span>.endIndex)</div></pre></td></tr></table></figure>
<h3 id="处理AccessToken"><a href="#处理AccessToken" class="headerlink" title="处理AccessToken"></a>处理AccessToken</h3><h4 id="请求token"><a href="#请求token" class="headerlink" title="请求token"></a>请求token</h4><p>5个必选参数<br><img src="/images/14804086315231.jpg" width="726"></p>
<p>获取code之后立即发起token的网络请求 , 请求成功后保存到模型中 , 由于网络工具类直接返回 SwiftyJSON 对象 , 则可以通过 ObjectMapper (<a href="https://github.com/Hearst-DD/ObjectMapper" target="_blank" rel="external">ObjectMapper</a>) 转为模型对象</p>
<p><img src="/images/14804086422144.jpg" width="712"></p>
<p>返回数据为字典,调用 json.dictionaryObject 方法</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/// 请求Token信息</span></div><div class="line"><span class="comment">///</span></div><div class="line"><span class="comment">/// - Parameters:</span></div><div class="line"><span class="comment">///   - code: code description</span></div><div class="line"><span class="comment">///   - callBack: callBack description</span></div><div class="line"><span class="function"><span class="keyword">func</span> <span class="title">requestAccessToken</span><span class="params">(code: String, callBack: @escaping <span class="params">(WBUserInfo?)</span></span></span> -&gt; ())&#123;</div><div class="line">    </div><div class="line">    <span class="keyword">let</span> urlString = <span class="string">"https://api.weibo.com/oauth2/access_token"</span></div><div class="line"></div><div class="line">    <span class="keyword">let</span> paramenters = [</div><div class="line">        <span class="string">"client_id"</span>: client_id,</div><div class="line">        <span class="string">"client_secret"</span>: client_secret,</div><div class="line">        <span class="string">"grant_type"</span>: <span class="string">"authorization_code"</span>,</div><div class="line">        <span class="string">"code"</span>: code,</div><div class="line">        <span class="string">"redirect_uri"</span>: redirect_uri</div><div class="line">    ]</div><div class="line">    </div><div class="line">    <span class="type">HTTPTool</span>.shared.requestWithParameters(url: urlString, method: .post, parameters: paramenters, success: &#123; (json) <span class="keyword">in</span></div><div class="line">        <span class="keyword">let</span> userInfo = <span class="type">Mapper</span>&lt;<span class="type">WBUserInfo</span>&gt;().<span class="built_in">map</span>(<span class="type">JSONObject</span>: json.dictionaryObject)</div><div class="line">        callBack(userInfo)</div><div class="line">    &#125;) &#123; </div><div class="line">        </div><div class="line">    &#125;</div><div class="line">    </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="token本地化"><a href="#token本地化" class="headerlink" title="token本地化"></a>token本地化</h4><p>模型转字典</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> userData = userInfo.toJSON()</div></pre></td></tr></table></figure>
<p>存储</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">func</span> <span class="title">saveUserDefault</span><span class="params">(dict:[String: Any])</span></span> &#123;</div><div class="line">    userDict = dict</div><div class="line">    <span class="type">UserDefaults</span>.standard.<span class="keyword">set</span>(dict, forKey: userInfoKey)</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>读取</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">func</span> <span class="title">loadUserDefault</span><span class="params">()</span></span> -&gt; [<span class="type">String</span>: <span class="type">Any</span>]? &#123;</div><div class="line">    <span class="keyword">if</span> <span class="keyword">let</span> data = <span class="type">UserDefaults</span>.standard.dictionary(forKey: userInfoKey)&#123;</div><div class="line">        <span class="keyword">return</span> data</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="literal">nil</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>根据token存储状态判断登录</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">func</span> <span class="title">isLogin</span><span class="params">()</span></span> -&gt; <span class="type">Bool</span> &#123;</div><div class="line">    <span class="keyword">if</span> <span class="keyword">let</span> userDict = loadUserDefault() &#123;</div><div class="line">        <span class="keyword">let</span> isToken = userDict[<span class="string">"access_token"</span>] != <span class="literal">nil</span></div><div class="line">        <span class="keyword">return</span> isToken</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="literal">false</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Swift/" rel="tag"># Swift</a>
          
            <a href="/tags/OAuth/" rel="tag"># OAuth</a>
          
            <a href="/tags/微博/" rel="tag"># 微博</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/11/10/iOS-Simulator-slow/" rel="next" title="一条命令解决 10.12 下 iOS模拟器滑动卡顿">
                <i class="fa fa-chevron-left"></i> 一条命令解决 10.12 下 iOS模拟器滑动卡顿
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2015/12/09/Say-Goodbye/" rel="prev" title="《Say Goodbye 不是永远》By ChiliTomato">
                《Say Goodbye 不是永远》By ChiliTomato <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          
            <img class="site-author-image" itemprop="image"
              src="/images/avatar.jpg"
              alt="Dee" />
          
            <p class="site-author-name" itemprop="name">Dee</p>
            <p class="site-description motion-element" itemprop="description">AN Ordinary Developer , Geek</p>
        </div>

        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives/">
            
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">posts</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">7</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">21</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/didez" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-globe"></i>GitHub</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://wpa.qq.com/msgrd?v=3&uin=305451372&site=qq&menu=yes" target="_blank" title="QQ">
                  
                    <i class="fa fa-fw fa-globe"></i>QQ</a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.upyun.com" title="UPYUN" target="_blank">UPYUN</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.v2ex.com/?r=qq30545" title="V2EX" target="_blank">V2EX</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#网络请求"><span class="nav-number">1.</span> <span class="nav-text">网络请求</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Alamofire-SwiftyJSON"><span class="nav-number">1.1.</span> <span class="nav-text">Alamofire + SwiftyJSON</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#OAuth认证"><span class="nav-number">2.</span> <span class="nav-text">OAuth认证</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#WebView加载授权网页"><span class="nav-number">2.1.</span> <span class="nav-text">WebView加载授权网页</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#OAuth2的authorize接口"><span class="nav-number">2.1.1.</span> <span class="nav-text">OAuth2的authorize接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取URL"><span class="nav-number">2.1.2.</span> <span class="nav-text">获取URL</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#加载授权网页"><span class="nav-number">2.1.3.</span> <span class="nav-text">加载授权网页</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#截取code"><span class="nav-number">2.2.</span> <span class="nav-text">截取code</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#WebView代理"><span class="nav-number">2.2.1.</span> <span class="nav-text">WebView代理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#截取code字段"><span class="nav-number">2.2.2.</span> <span class="nav-text">截取code字段</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#处理AccessToken"><span class="nav-number">2.3.</span> <span class="nav-text">处理AccessToken</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#请求token"><span class="nav-number">2.3.1.</span> <span class="nav-text">请求token</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#token本地化"><span class="nav-number">2.3.2.</span> <span class="nav-text">token本地化</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2013 &mdash; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Dee</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" href="https://hexo.io">Hexo</a></div>

  <span class="post-meta-divider">|</span>

  <div class="theme-info">Theme &mdash; <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.0.2</div>


        




  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=59029013";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>


  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  


  




	





  





  








  





  

  

  

  

  

  

</body>
</html>
